//
//  CalendarManager.m
//  testRN
//
//  Created by Juvid on 2017/3/22.
//  Copyright © 2017年 Facebook. All rights reserved.
//

#import "CalendarManager.h"

@implementation CalendarManager
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(juEvent:(NSString *)name location:(NSString *)location)
{
  RCTLogInfo(@"原生的 %@ at %@", name, location);
  CalendarManager *test=[[CalendarManager alloc]init];
  [test juTest];
}
RCT_EXPORT_METHOD(findCall:(RCTResponseSenderBlock)callback)
{
  NSArray *events = @[@"1235",@"76543"];
  callback(@[[NSNull null], events]);
}

//RCT_REMAP_METHOD
RCT_EXPORT_METHOD(executeCommand:(NSDictionary *)cmdDic
                 resolver:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject)
{
  NSArray *events = @[@"1235",@"76543"];
  if (resolve) {
      resolve( events);
  }
}
RCT_REMAP_METHOD(alertUserPromise, resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)reject){
  _resolveBlock=resolver;
  _rejectBlock=reject;
  dispatch_async(dispatch_get_main_queue(), ^{
    UIAlertView * alertView=[[UIAlertView alloc] initWithTitle:@"react-native" message:@"使用Promise？" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"继续", nil];
    [alertView show];
  });
}

RCT_EXPORT_METHOD(showAlertAndCallback:(RCTResponseSenderBlock)callback){
//  _alertCallback=callback;
  UIAlertView * alertView=[[UIAlertView alloc] initWithTitle:@"react-native" message:@"是否继续？" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"继续", nil];
  [alertView show];

  
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  if (buttonIndex==0) {
    NSError * err=[NSError errorWithDomain:@"test" code:0 userInfo:nil];
    _rejectBlock(@"0",@"cancel",err);
  }else{
    _resolveBlock(@[@1]);
  }
}
-(void)juTest{
 NSLog(@"原生的1234");
}
- (NSArray<NSString *> *)supportedEvents
{
  return @[@"EventReminder"];
}

- (void)calendarEventReminderReceived:(NSNotification *)notification
{
  NSString *eventName = notification.userInfo[@"name"];
  [self sendEventWithName:@"EventReminder" body:@{@"name": eventName}];
}

@end
